﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ChiDao.Component.Utils;
using ChiDao.Business.Models.Account;
using ChiDao.Site.Models;

using Webdiyer.WebControls.Mvc;
using ChiDao.Business.Service;

namespace ChiDao.Site.Web.Controllers
{
    [Export]
    public class OrgController : BaseController
    {
        [Import]
        public IOrgService OrgContract { get; set; }

        public ActionResult Index(int? id)
        {
            int pageIndex = id ?? 1;
            const int pageSize = 20;
            PropertySortCondition[] sortConditions = new[] { new PropertySortCondition("Id") };
            int total;

            var views = OrgContract.Orgs.Where<Org, Guid>(m => true, pageIndex, pageSize, out total, sortConditions).Select(m => new OrgView
            {
                ID = m.ID,
                OrgNo = m.OrgNo,
                OrgName = m.OrgName,
                PID = m.PID
            });

            //使用强制转换会报以下错误：
            //无法将类型“ChiDao.Business.Models.Account.Department”强制转换为类型“ChiDao.Site.Models.DepartmentView”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型
            //var models = DepartmentContract.Departments.Where<Department, Guid>(m => true, pageIndex, pageSize, out total, sortConditions);
            //IQueryable<DepartmentView> views = models.Cast<DepartmentView>();

            PagedList<OrgView> model = new PagedList<OrgView>(views, pageIndex, pageSize, total);
            return View(model);
        }

        public ActionResult Add(OrgView viewModel)
        {
            OrgContract.Add(viewModel);
            return View();
        }

        public ActionResult Query(OrgView viewModel)
        {
            return View();
        }



    }
}
